package com.real.operationalcontrols.适配器;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.real.operationalcontrols.R;
import com.real.operationalcontrols.实体类.WuRanYuanCompanyBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CompanyAdapter extends BaseExpandableListAdapter implements Filterable {
    Context context;
    List<WuRanYuanCompanyBean.FactoryBean> data = new ArrayList<>();
    //这个数据是会改变的，所以要有个变量来备份一下原始数据
    private List<WuRanYuanCompanyBean.FactoryBean> oldBeanList = new ArrayList<>(); //用来备份原始数据

    MyFilter mFilter;

    public CompanyAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<WuRanYuanCompanyBean.FactoryBean> data) {
        this.data = data;
        this.oldBeanList = data;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return data.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return data.get(i).getDevice().size();
    }

    @Override
    public Object getGroup(int i) {
        return data.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return data.get(i).getDevice().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
        GroupViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.wry_company_layout, viewGroup,
                    false);
            holder = new GroupViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (GroupViewHolder) view.getTag();
        }
        holder.parentCompany.setText(data.get(groupPosition).getName());

        if (b) {
            holder.parentImage.setRotation(90);
        } else {
            holder.parentImage.setRotation(0);
        }
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean b, View view,
                             ViewGroup viewGroup) {

        ChildViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.wry_drive_layout, viewGroup,
                    false);
            holder = new ChildViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ChildViewHolder) view.getTag();
        }
        holder.chidrenDrive.setText(data.get(groupPosition).getDevice().get(childPosition).getName());
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new MyFilter();
        }
        return mFilter;
    }

    class GroupViewHolder {
        @BindView(R.id.parent_company)
        TextView parentCompany;
        @BindView(R.id.parent_image)
        ImageView parentImage;

        GroupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    class ChildViewHolder {
        @BindView(R.id.chidren_drive)
        TextView chidrenDrive;

        ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    //我们需要定义一个过滤器的类来定义过滤规则
    class MyFilter extends Filter {
        //我们在performFiltering(CharSequence charSequence)这个方法中定义过滤规则
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults result = new FilterResults();
            List<WuRanYuanCompanyBean.FactoryBean> list;
            if (TextUtils.isEmpty(charSequence)) {//当过滤的关键字为空的时候，我们则显示所有的数据
                list = oldBeanList;
            } else {//否则把符合条件的数据对象添加到集合中
                list = new ArrayList<>();
                for (WuRanYuanCompanyBean.FactoryBean citysBean : oldBeanList) {
                    if (citysBean.getName().contains(charSequence)) { //要匹配的item中的view
                        list.add(citysBean);
                    }

                }
            }
            result.values = list; //将得到的集合保存到FilterResults的value变量中
            result.count = list.size();//将集合的大小保存到FilterResults的count变量中

            return result;
        }

        //在publishResults方法中告诉适配器更新界面
        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            data = (List<WuRanYuanCompanyBean.FactoryBean>) filterResults.values;
            if (filterResults.count > 0) {
                notifyDataSetChanged();//通知数据发生了改变
            } else {
                notifyDataSetInvalidated();//通知数据失效
            }
        }
    }
}
